//获取应用实例
const app = getApp()
const request = require('../../../utils/request');
var ToType = '';
//请求接口函数
function getOrders(that,page,limit,ToType){
  request.HttpRequst(true, 'user/myOrder', false, '', { "token": wx.getStorageSync("sessionId"), "page": page, "limit": limit, "type": ToType,'order_type':[1] }, "post", false, res => {
      console.log(res)
      if (res.code == 200) {//返回有数据
        that.setData({
          orders: res.data.data
        })
      } else {//返回错误（弹窗提示）
        if(res.info != 'empty'){
            wx.showModal({
                title: '提示',
                content: res.info,
                success:function() {
                    wx.navigateTo({
                        url: '../myLogin/myLogin'
                    })
                }
            })
        }else{
            that.setData({
                orders:''
            })
        }
      }
    })
}

//下拉加载数据
function loadingData(that,page,limit,ToType){
    if(that.data.upflag){
        request.HttpRequst(true, 'user/myOrder', false, '', { "token": wx.getStorageSync("sessionId"), "page": page, "limit": limit, "type": ToType,'order_type':[1] }, "post", false, res => {
            console.log(res.data)
            if(res.data != ''){
                let ordersData = that.data.orders.concat(res.data.data)
                that.setData({
                    orders: ordersData
                })
            }else{
                that.setData({
                    upflag: false
                })
            }
        })
    }
    
}
Page({
    //初始化数据
    data: {
        orderTab: ['全部','未支付','支付成功','进行中','已购买','退款/售后'],
        currentId: 0,
        page:1,
        limit:10,
        orders:'',
        ToType:'',
        upflag: true,
        fromType: 0,
        imgUrl:request.imgUrl
    },
    onReady: function() {
        //页面标题
        wx.setNavigationBarTitle({
            title: '我的订单'
        })
        var that = this
        getOrders(that, that.data.page, that.data.limit,that.data.ToType)
    },
    //页面打开获取10条数据
    // onShow: function(){
    //     var that = this
    //     //打开获取数据
    //     getOrders(that, that.data.page, that.data.limit,ToType)
    // },

    //点击状态获取数据
    clickOrderTab(e) {
        //处理状态
        var that = this
        var fromType = e.target.dataset.id
        that.setData({
            fromType: fromType,
            upflag: true
        })
        that.data.page = 1;
        if(fromType==0){
            that.data.ToType = ''
        }else if(fromType==1){
            that.data.ToType = 1
        }else if(fromType==2){
            that.data.ToType = 2
        }else if(fromType==3){
            that.data.ToType = 3
        }else if(fromType==4){
            that.data.ToType = 4
        }else if(fromType==5){
            that.data.ToType = -1
        }
       console.log(that.data.ToType)
       //打开获取数据
       getOrders(that, that.data.page,that.data.limit,that.data.ToType)
    },

    //下拉获取更多数据
    onReachBottom: function () {
       var that = this
       that.data.page++
       loadingData(that, that.data.page,that.data.limit,that.data.ToType)
    },
    //跳到订单详情
    goDetail(e){
        console.log(e.currentTarget.dataset.order_type)
        wx.navigateTo({
          url: 'detail/detail?id=' + e.currentTarget.dataset.id + '&order_type=' + e.currentTarget.dataset.order_type
        })
    },
    
})